package com.hua.api.core.framework.util;

import com.hua.api.core.util.StringUtil;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@SuppressWarnings("all")
public class DateUtil {
    private static Long DAY_TIMES = 24 * 60 * 60 * 1000l;
    private static Long HOUR_TIMES = 60 * 60 * 1000l;
    private static Long MIN_TIMES = 60 * 1000l;
    private static Long SECOND_TIMES = 1000l;

    public static String FORMAT_YYMMDD = "yyyy-MM-dd";
    public static String FORMAT_YYMMDDHHMMSS = "yyyy-MM-dd HH:mm:ss";
    public static String FORMAT_YYMMDDHHMMSSSSS = "yyyy-MM-dd HH:mm:ss:sss";
    private static final DateFormat[] ACCEPT_DATE_FORMATS = {
            new SimpleDateFormat("yyyy-MM-dd"),
            new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"),
            new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS"),
            new SimpleDateFormat("yyyy/MM/dd"),
            new SimpleDateFormat("yyyy.MM.dd"),
            new SimpleDateFormat("yyyyMMdd"),
            new SimpleDateFormat("dd/MM/yyyy"),
            // new SimpleDateFormat("yyyy-MM-ddTHH:mm:ss+0800")
    };// 支持转换的日期格式

    public static Date getDate(Long date) {
        return null == date ? null : new Date(date);
    }

    public static Date unixToDate(Long unixTime) {
        return null == unixTime ? null : new Date(unixTime * 1000);
    }

    public static Long dateToUnix(Date date) {
        return null == date ? null : date.getTime() / 1000;
    }

    public static Date parse(String dateStr) {
        if (dateStr == null || "".equals(dateStr))
            return null;
        for (DateFormat format : ACCEPT_DATE_FORMATS) {
            try {
                return format.parse(dateStr);
            } catch (Exception e) {
                continue;
            }
        }
        return null;
    }

    public static Date parse(String dateStr, String format) {
        if (StringUtil.isEmpty(dateStr)) {
            return null;
        }
        format = StringUtil.isEmpty(format) ? FORMAT_YYMMDD : format;
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date d = null;
        try {
            d = sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return d;
    }

    public static Date parse(Date cdate, String strFormat) {
        String date = getFormatDate(cdate, strFormat);
        return parse(date, "yyyy-MM-dd HH:mm:ss");
    }

    public static String getFormatDate(Date d, String format) {
        format = StringUtil.isEmpty(format) ? FORMAT_YYMMDD : format;
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return (d == null) ? "" : sdf.format(d);
    }

    public static String getYesterdayStr(String dateFormat) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -1);
        Date time = calendar.getTime();
        String yesterday = new SimpleDateFormat(dateFormat).format(time);
        return yesterday;
    }

    public static Date beginOfDate(Date d){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(d);
        calendar.set(Calendar.HOUR_OF_DAY,0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        calendar.set(Calendar.MILLISECOND,0);
        return calendar.getTime();
    }
    public static Date endOfDate(Date d) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(d);
        calendar.set(Calendar.HOUR_OF_DAY,23);
        calendar.set(Calendar.MINUTE,59);
        calendar.set(Calendar.SECOND,59);
        calendar.set(Calendar.MILLISECOND,999);
        return calendar.getTime();
       // return d != null ? DateUtil.parse(DateUtil.getFormatDate(d, "yyyy-MM-dd 23:59:59:999"), FORMAT_YYMMDDHHMMSSSSS) : null;
    }



    public static int getWeek(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.WEEK_OF_YEAR);
    }

    public static Date offsetDate(Date date, int day) {
        if (date == null) {
            return new Date();
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DAY_OF_YEAR, day);
        return cal.getTime();
    }

    public static Date offsetHour(Date date, int hour) {
        if (date == null) {
            return new Date();
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.HOUR, hour);
        return cal.getTime();
    }

    public static Date offsetMonth(Date date, int Month) {
        if (date == null) {
            return new Date();
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, Month);
        return cal.getTime();

    }

    public static Date offsetYear(Date date, int Year) {
        if (date == null) {
            return new Date();
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.YEAR, Year);
        return cal.getTime();

    }


    public static Date offsetWeek(Date date, int Week) {
        if (date == null) {
            return new Date();
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.WEEK_OF_YEAR, Week);
        return cal.getTime();

    }

    public static boolean beforeDate(Date currentDate, Date comDate) {
        if (comDate == null || currentDate == null) {
            return false;
        } else {
            return currentDate.before(comDate);
        }
    }



    public static Long getNumbersByTwoDate(Date bDate, Date eDate, Long parameter) {
        parameter = StringUtil.isEmpty(parameter) ? DAY_TIMES : parameter;

        return (eDate.getTime() - bDate.getTime()) / parameter;
    }

    public static int getMonthSpace(Date date1, Date date2) {

        int result = 0;
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        c1.setTime(date1);
        c2.setTime(date2);
        result = c2.get(Calendar.MONTH) - c1.get(Calendar.MONTH);

        return result == 0 ? 1 : Math.abs(result);

    }

    public static Long getDayBytwoDate(Date bDate, Date eDate) {
        return getNumbersByTwoDate(bDate, eDate, DAY_TIMES);
    }

    public static Long getHourByTwoDate(Date bDate, Date eDate) {
        return getNumbersByTwoDate(bDate, eDate, HOUR_TIMES);
    }

    public static Long getMinByTwoDate(Date bDate, Date eDate) {
        return getNumbersByTwoDate(bDate, eDate, MIN_TIMES);
    }

    public static Long getSecondByTwoDate(Date bDate, Date eDate) {
        return getNumbersByTwoDate(bDate, eDate, SECOND_TIMES);
    }

    public static Date getFristDayOfWeek() {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.DAY_OF_WEEK, 1);
        return c.getTime();
    }

    public static Date getFistDayOfMonth(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), 1, 0, 0, 0); // 0时0分0秒
        return c.getTime();
    }

    public static Date getLastDayOfMonth(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
        return c.getTime();
    }

    public static Date getFistDayOfMonth() {
        Calendar c = Calendar.getInstance();
        // c.set(Calendar.DAY_OF_MONTH, 1);
        c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), 1, 0, 0, 0); // 0时0分0秒
        return c.getTime();
    }

    public static Date getFistDayOfYear(Date date) {
        Calendar c = Calendar.getInstance();
        if (null != date) {
            c.setTime(date);
        }
        c.set(Calendar.DAY_OF_YEAR, 1);
        return c.getTime();

    }

    public static Date getFistDayOfYear() {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.DAY_OF_YEAR, 1);

        return c.getTime();
    }

    public static Date getAddNumbersDay(Date date, int i, Long parameter) {
        if (date == null) {
            return null;
        } else {
            parameter = StringUtil.isEmpty(parameter) ? DAY_TIMES : parameter;
            date.setTime(date.getTime() + i * parameter);
            return date;
        }
    }

    public static Date getAddDayByDay(Date date, int i) {
        return getAddNumbersDay(date, i, DAY_TIMES);
    }

    public static Date getAddHourByDate(Date date, int i) {
        return getAddNumbersDay(date, i, HOUR_TIMES);
    }

    public static Date getAddMinByDate(Date date, int i) {
        return getAddNumbersDay(date, i, MIN_TIMES);
    }


    public static int getMinute() {
        Calendar cal = Calendar.getInstance();
        return cal.get(Calendar.MINUTE);
    }


    public static int getMonthOfYear() {
        Calendar cal = Calendar.getInstance();
        return cal.get(Calendar.MONTH);
    }

    public static Date addDate(Date date, int i) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, i);
        return cal.getTime();
    }

    public static Date addMonth(Date date, int i) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, i);
        return cal.getTime();
    }

    /**
     * 获取本年度最后一天0点的时间戳
     *
     * @return
     */
    public static Date lastDateOfYear() {
        Calendar currCal = Calendar.getInstance();
        int currentYear = currCal.get(Calendar.YEAR);
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, ++currentYear);
        calendar.add(Calendar.DAY_OF_YEAR, -1);
        return calendar.getTime();

    }

    /**
     * 获取当前小时的上一小时
     *
     * @return
     */
    public static String getLastHour() {
        Calendar calendar = Calendar.getInstance();
        /* HOUR_OF_DAY 指示一天中的小时 */
        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - 1);
        SimpleDateFormat df = new SimpleDateFormat("HH");
        return df.format(calendar.getTime());
    }

    /**
     * 获取当前小时值
     *
     * @return
     */
    public static String getCurrentHour() {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat df = new SimpleDateFormat("HH");
        return df.format(calendar.getTime());
    }

//    public static void main(String[] args) {
//
//        System.out.println(DateUtil.getFormatDate(new Date(),"HH:mm"));
//
//
//    }



}
